<template>
  <div>
    <tableComponents
      :pagination="{currentPage,pageSize:pagesize,total:card_list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="receive_search"
      :searchData="userSearchData"
      :isHaveExport="false"
      :isHaveAdd="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="card_list.data"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="头像" min-width="80">
            <template slot-scope="scope">
              <div class="img_bor_tx">
                <img :src="$fnc.getImgUrl(scope.row.avatar)" alt />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="用户信息" min-width="190">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>用户名：</span>
                  <span>{{scope.row.username || '----'}}</span>
                </div>
                <div>
                  <span>用户昵称：</span>
                  <span>{{scope.row.nickname || '----'}}</span>
                </div>
                <div>
                  <span>订单号：</span>
                  <span>{{scope.row.oid}}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品信息" min-width="250">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>商品标题：</span>
                  <span>{{scope.row.title || '----'}}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="密匙" min-width="250">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>{{scope.row.key}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="状态" min-width="100">
            <template slot-scope="scope">
              <div class="tab_div">
                <p v-if="scope.row.status == 0">
                  <el-tag effect="dark">未发放</el-tag>
                </p>
                <p v-else-if="scope.row.status == 1">
                  <el-tag effect="dark">已发放</el-tag>
                </p>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="80">
            <template slot-scope="scope">
              <el-button type="danger" size="mini" @click="delete_card(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteAll_card">删除</a>
      </template>
    </tableComponents>
  </div>
</template>
<script>
import { shopCardSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  name: "card",
  data() {
    return {
      userSearchData: shopCardSearchData,
      multipleSelection: [],
      serarch_from: {},
      is_search: false,
      loading: false,
      card_list: {},
      currentPage: 1, //初始页
      pagesize: 10 //    每页的数据
    };
  },

  created() {
    this.get_card_list(1);
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delete_card(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops.del_cardlist({ id_str: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_card_list();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
              this.get_card_list();
            }
          });
        }
      });
    },
    deleteAll_card() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            this.$api.getShops.del_cardlist({ id_str: str }).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.get_card_list();
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
                this.get_card_list();
              }
            });
          }
        });
      }
    },
    receive_search(val) {
      this.is_search = true;
      this.serarch_from = val;
      this.search_card_list(1);
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      //每页下拉显示数据

      this.pagesize = size;
      this.currentPage = 1;
      this.get_card_list(1);
    },
    handleCurrentChange: function(currentPage) {
      //点击第几页
      this.currentPage = parseInt(currentPage);
      if (this.is_search) {
        //是搜索
        this.search_card_list(this.currentPage);
      } else {
        //不是搜索
        this.get_card_list(this.currentPage);
      }
    },
    search_card_list(page) {
      this.loading = true;
      var params = this.serarch_from;
      params.page = page;
      params.page_size = this.pagesize;
      this.$api.getShops.get_cardlist(params).then(res => {
        if (res.data.code == 200) {
          this.card_list = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    get_card_list(page) {
      this.loading = true;
      this.$api.getShops
        .get_cardlist({ page: page, page_size: this.pagesize })
        .then(res => {
          if (res.data.code == 200) {
            this.card_list = res.data.result;
            this.loading = false;
          } else {
            this.$fnc.alertError(res.data.result);
            this.loading = false;
          }
        });
    }
  }
};
</script>
<style  scoped>
</style>
